/*栈 基于顺序表 main函数未实现程序逻辑
*vs2010编译
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define elementType int
#define status int
#define TRUE 1
#define FALSE 0

typedef struct
{
    elementType* element;
	elementType *top;
	elementType *bottom;
	int maxLength;
	int usedLength;
}Stack;
Stack *createAStack();
status enlargeStack(Stack *);
status insertAElement(Stack *, elementType);
elementType getAElement(Stack *);

int main()
{
	Stack *stack = createAStack();
	//TODO:程序逻辑未实现，但函数已经实现，请自行验证

	return 0;
}

Stack *createAStack()	//创建一个新的栈并分配内存空间
{
	Stack *stack = new Stack;
	stack->maxLength = 100;
	stack->usedLength = 0;
	stack->element = (elementType *)malloc(sizeof(elementType) * stack->maxLength);
	stack->top = stack ->element;
	stack->bottom = stack ->element;
	return stack;
}

status enlargeStack(Stack *stack)	//扩展存储空间
{
	if (stack != NULL)
	{
		stack->maxLength += 100;
		stack->element = (elementType *)realloc(stack, sizeof(elementType) * stack->maxLength);
		if (stack->element != NULL) return TRUE;
		else return FALSE;
	}
	else
	{
		return FALSE;
	}
}

status insertAElement(Stack *stack, elementType element)
{
	if (stack->top != NULL)
	{
		stack->usedLength++;
		if (stack->usedLength == stack->maxLength)
			if (enlargeStack(stack) == FALSE) exit(1);
		stack->top++;
		*stack->top = element;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

elementType getAElement(Stack *stack)
{
	if (stack->top != stack->bottom)
	{
		elementType element = *stack->top;
		stack->top--;
		stack->usedLength--;
		return element;
	}
	else
	{
		return FALSE;
	}
}